//
//  main.m
//  省市区
//
//  Created by dllo on 16/5/27.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    
    NSString *str =[NSString stringWithContentsOfFile:@"/Users/dllo/Desktop/gongcheng/省市区/area.txt" encoding:NSUTF8StringEncoding error:nil];
   //中文转化为二进制代码
//    NSLog(@"%@",str);
//    NSArray *array = @[@"刘奥博",@"李和晨",@"司昌宝",@"李杰"];
//    
//    NSData *data = [NSJSONSerialization dataWithJSONObject:array options:NSJSONWritingPrettyPrinted error:nil];
//    
//    
//    NSString *str2 = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//    
//    NSLog(@"%@",str2);
    
#if 1
    
    
    NSArray *arraySum = [str componentsSeparatedByString:@"\n"];
    
    
    NSMutableArray *china = [NSMutableArray arrayWithCapacity:1];
    
    

    NSInteger countProvince = -1;
    NSInteger countCity = -1;
//    NSInteger c = 0;
    for(NSString *areaNow in arraySum){
        if([areaNow hasPrefix:@"    "]){
            //将区名string放入areaKey对应的数组中
            //[china[countProvince] ]
            [[[[[china objectAtIndex:countProvince] valueForKey:@"cityKey"] objectAtIndex:countCity] valueForKey:@"areaKey"] addObject:areaNow];
            
            
        }else if([areaNow hasPrefix:@"  "]){
            NSMutableArray *area = [NSMutableArray arrayWithCapacity:1];
            NSDictionary *cityDict = [NSDictionary dictionaryWithObjectsAndKeys:areaNow,@"cityName",area,@"areaKey",nil];
            [[[china objectAtIndex:countProvince] valueForKey:@"cityKey"] addObject:cityDict];
            countCity++;
            
            
        }else if(![areaNow hasPrefix:@"  "]){
            NSMutableArray *city = [NSMutableArray arrayWithCapacity:1];
            NSDictionary *province = [NSDictionary dictionaryWithObjectsAndKeys:areaNow,@"provinceName",city,@"cityKey",nil];
            //将省份字典添加进中国数组中
            [china addObject:province];
            countProvince++;
            countCity = -1;
        }

    }
    
#if 1
    //输出方法3.0. //遍历输出
    for(NSDictionary *dictProvince in china){
        NSLog(@"%@",[dictProvince objectForKey:@"provinceName"]);
        for(NSDictionary *dictCity in [dictProvince objectForKey:@"cityKey"]){
            NSLog(@"%@",[dictCity objectForKey:@"cityName"]);
            for(NSString *strArea in [[[dictProvince objectForKey:@"cityKey"] lastObject] objectForKey:@"areaKey"]){
                    NSLog(@"%@",strArea);
            }
        }
    }
    
#endif
    
#if 0
    //输出方法1.0   //似乎字典的输出顺序不太靠谱
    NSData *data = [NSJSONSerialization dataWithJSONObject:china options:NSJSONWritingPrettyPrinted error:nil];
    NSString *strChina = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"%@",strChina);
    
#endif
    
#if 0
    //输出方法2.0  //与直接输出数组并无差别.还是字典内的key value 输出顺序无法反向
    
    NSEnumerator *enumer = [china objectEnumerator];
    id value = nil;
    while(value = [enumer nextObject]){
        NSData *dataValue = [NSJSONSerialization dataWithJSONObject:value options:NSJSONWritingPrettyPrinted error:nil];
        NSString *strValue = [[NSString alloc] initWithData:dataValue encoding:NSUTF8StringEncoding];
        NSLog(@"%@",strValue);
    }

#endif

    
//    for (NSString *strkk in china) {
//        
//        NSLog(@"%@",strkk);
//    }
//    NSData *dataArea = [NSJSONSerialization dataWithJSONObject:[[[[china objectAtIndex:countProvince] valueForKey:@"cityKey"] objectAtIndex:countCity] valueForKey:@"areaKey"] options:NSJSONWritingPrettyPrinted error:nil];
//    
//    NSString *strArea = [[NSString alloc] initWithData:dataArea encoding:NSUTF8StringEncoding];
//    
//    NSLog(@"%@",strArea);
    
    
    
    
    
//    NSString *str2 = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//    NSString *strChina = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
 //   NSLog(@"%@",strChina);
//    for(NSString *strReturn in china){
//        NSLog(@"%@",strReturn);
//    }
    
    
//    //尝试输出
//    for (int i = 0;i < china.count;i++){
//        NSDictionary *dictReturn = [china objectAtIndex:i];
//        dictReturn.allValues
//    }
    
#endif
    
    
    
 //   NSMutableArray *china =
    return 0;
}
